package com.sinodata.bsm.cicp.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

import com.sinodata.bsm.cicp.util.pool.SSH2ConnPool;
import com.sinodata.bsm.cicp.util.telnet.EthzSSH;

public class SSH2PoolTest implements Runnable {

    int tn = 1;
    long tt = 0;

    public SSH2PoolTest(int i, long t1) {
        tn = i;
        tt = t1;
    }

    public void run() {
        int maxConnSize = 5;
        String host = "10.200.200.1";
        int port = 22;
        String user = "bsm";
        String pwd = "bsm123";
        String cmd = "datee \n";
        //        String cmd = "date";
        EthzSSH ssh2 = null;
        Session session = null;
        Connection conn = null;
        BufferedReader br = null;
        BufferedReader be = null;

        for (int i = 1; i <= 1; i++) {
            try {
                ssh2 = SSH2ConnPool.getInstance().getConnection(host, port, user, pwd, maxConnSize);
                conn = ssh2.getConn();
                System.out.println("---start----");
                session = conn.openSession();
                session.execCommand(cmd);
                StringBuilder sb = new StringBuilder();
                String tmpStr = "";
                //获取标准错误
                InputStream stderr = new StreamGobbler(session.getStderr());
                be = new BufferedReader(new InputStreamReader(stderr, "GBK"));
                while ((tmpStr = be.readLine()) != null) {
                    sb.append(tmpStr + "\n");
                }
                //获取标准输出
                InputStream stdout = new StreamGobbler(session.getStdout());
                br = new BufferedReader(new InputStreamReader(stdout, "GBK"));
                while ((tmpStr = br.readLine()) != null) {
                    sb.append(tmpStr + "\n");
                }

                System.out.println(sb.toString());
                System.out.println("---end---");
            } catch (Exception e) {
                e.printStackTrace();
                ssh2.setValid(false);
            } finally {
                session.close();
                try {
                    if (ssh2 != null) {
                        SSH2ConnPool.getInstance().free(host, ssh2);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (i == 5) {
                Date t2 = new Date();
                System.out.println("115第" + tn + "个线程执行耗时" + (t2.getTime() - tt) + "ms-----------------------------------");
                System.out.println("毫秒数:" + t2.getTime());
            }
        }
    }

    public static void main(String[] args) {
        Date t1 = new Date();
        for (int j = 1; j <= 1; j++) {
            Thread t = new Thread(new SSH2PoolTest(j, t1.getTime()));
            t.start();
        }

    }

}
